package com.ttloner.admin.controller;

import com.ttloner.admin.bean.City;
import com.ttloner.admin.bean.User;
import com.ttloner.admin.mapper.UserMapper;
import com.ttloner.admin.service.imp.CityServiceImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author TTLoner
 * @date 2021/7/19 20:13
 * @description
 */
@Slf4j
@Controller
public class IndexController {
    //登录页面
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CityServiceImp cityServiceImp;
    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }
    @PostMapping("/main")
    public String main(User user,HttpSession session,Model model){
        String name = user.getUserName();
        String thisPassword = userMapper.selectPasswordByName(name);
        if(user.getPassword().equals(thisPassword)&&thisPassword!=null){
            session.setAttribute("loginUser",user);
        }else {
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }
        //使用重定向防止重复提交，由于不能同时post和map需要增加一层跳转
        return "redirect:/main.html";
    }
    @GetMapping("/main.html")
    public String toMain() {
        return "main";
    }

    @GetMapping("/regist")
    public String toRegist(){
        log.info("到此");
        return "registration";
    }
    @ResponseBody
    @GetMapping("/city")
    public String showCity(@RequestParam("id") Long id){
        City city = cityServiceImp.getById(id);
        return city.toString();
    }
}
